package server;

import java.io.File;
import java.net.InetSocketAddress;

import org.jdom.Element;

import server.business.http.controller.HttpController;
import server.commonData.IdFactory;
import server.jdbc.DAOUtil;
import server.utils.AppFile;
import server.utils.StringUtils;
import server.utils.XmlUtil;

import com.core.ChannelManager;
import com.core.EnginService;
import com.core.HttpAcceptor;
import com.core.NetAcceptor;
import com.core.thread.OneMessageExecutorPool;
/**
 * 服务器核心  
 * @author King
 *
 */
public class ServerEngin extends EnginService
{
	private static ServerEngin instance;
	
	/**监听**/
	private NetAcceptor acceptor;
	
	/**http**/
	private HttpAcceptor httpAcceptor;
	
	/**游戏缓存 游戏所有的内存对象**/
	private GameCache cache;
	
	public static final ServerEngin getInstance() {
		if (instance == null) {
			synchronized (ServerEngin.class) {
				if (null == instance) {
					instance = new ServerEngin();
				}
			}
		}
		return instance;
	}
	
	private ServerEngin()
	{
		
	}
	
	public void init(InetSocketAddress address,InetSocketAddress httpAddress)
	{
		if(executor==null)
			this.executor = new OneMessageExecutorPool();
		if(asyncService!=null)
			asyncService.init(this.executor, Runtime.getRuntime().availableProcessors()/2);
		if(address!=null)
			acceptor = new NetAcceptor(this, address);
		if(httpAddress!=null)
			httpAcceptor = new HttpAcceptor(this, httpAddress,HttpController.getHandlers());
		
		new ServerModuleRegistration(this.messageManager,this.handlerManager).registerModule();
		this.channelManager = new ChannelManager();
		this.cache = new GameCache();
		this.configLoader = new ServerConfigLoader();
		this.configLoader.starLoad();
		//初始化数据库
		DAOUtil.getInstance();
		//初始化脚本
		this.scriptM = new ServerScriptManager();
		this.scriptM.init();
	}
	
	public void initHttps(InetSocketAddress httpAddress)
	{
		this.init(null, httpAddress);
	}
	
	public void init(InetSocketAddress address)
	{
		this.init(address, null);
	}
	
	public static void main(String[] args)
	{
		
		if(args==null||args.length==0)
		{
			File f = new File(System.getProperty("user.dir"));
			String path = f.getPath() + File.separator + "src" + File.separator
					+ "main" + File.separator + "resources"+File.separator+"config";
			AppFile.path=path;
		}else{
			File f = new File(System.getProperty("user.dir"));
			String path = f.getPath() + File.separator+"config";
			AppFile.path=path;
		}
		System.out.println("path: "+AppFile.path);
		Element element = XmlUtil.getRootElement(AppFile.getServerConfig());
		int sid = Integer.parseInt(element.getChild("sid").getText());
		int zid = Integer.parseInt(element.getChild("zid").getText());
		String socketAddress = element.getChild("socketAddress").getText();
		String httpAddress = element.getChild("httpAddress").getText();
		
		IdFactory.init(zid, sid);
		
		ServerEngin instance = ServerEngin.getInstance();
		instance.init(StringUtils.parseAddress(socketAddress), StringUtils.parseAddress(httpAddress));
		
	}
	
	
	public HttpAcceptor getHttpAcceptor() {
		return httpAcceptor;
	}

	@Override
	public void shutdown() {
		super.shutdown();
		if(this.acceptor!=null)
			this.acceptor.shutdown();
	}

	public NetAcceptor getAcceptor() {
		return acceptor;
	}
	

	/**
	 * 获取游戏缓存
	 * 所有游戏的内存对象
	 * @return
	 */
	public GameCache getCache() {
		return cache;
	}
	
}
